package com.fzj.charging.order.service.impl;

import com.fzj.charging.common.pojo.JsonResult;
import com.fzj.charging.common.pojo.param.GunStatusUpdateParam;
import com.fzj.charging.common.uiils.JacksonUtil;
import com.fzj.charging.common.pojo.vo.StationInfoVO;
import com.fzj.charging.common.pojo.vo.UserInfoVO;
import com.fzj.charging.order.common.ChargingConstants;
import com.fzj.charging.order.common.GunStatusConstants;
import com.fzj.charging.order.common.MqttConstants;
import com.fzj.charging.order.feign.DeviceClient;
import com.fzj.charging.order.feign.UserClient;
import com.fzj.charging.order.mqtt.MqttProducer;
import com.fzj.charging.order.pojo.dto.ChargingDto;
import com.fzj.charging.order.pojo.dto.OrderAddParam;
import com.fzj.charging.order.pojo.po.OrderMQPO;
import com.fzj.charging.order.quartz.DeviceCheckJob;
import com.fzj.charging.order.rabbitmq.RabbitMQProducer;
import com.fzj.charging.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    UserClient userClient;
    @Autowired
    DeviceClient deviceClient;
    @Autowired
    MqttProducer mqttProducer;
    @Autowired
    RabbitMQProducer rabbitMQProducer;
    @Override
    public String createOrder(OrderAddParam orderAddParam) {
        log.debug("创建订单业务，参数：{}",orderAddParam);
        log.debug("创建订单业务-生成订单号");
        String orderNo = getOrderNo();
        log.debug("创建订单业务-生成订单号：{}",orderNo);

        updateGunStatus(orderAddParam.getGunId());
        UserInfoVO userInfo = getUserInfo(orderAddParam.getUserId());
        getStationInfo(orderAddParam.getGunId());
        startCharging(orderNo, orderAddParam.getPileId(), orderAddParam.getGunId(),userInfo.getUserId(),userInfo.getCardId());
        log.debug("给充电桩发送充电的指令，订单号：{}，桩id：{}，枪id：{}",orderNo,orderAddParam.getPileId(),orderAddParam.getGunId());

        log.debug("创建订单业务，启动设备自检定时任务");
        DeviceCheckJob deviceCheckJob = new DeviceCheckJob(orderNo, orderAddParam.getGunId());

        log.debug("给rabbitmq发送,延迟消息,订单号:{}",orderNo);
        Boolean sendRabbitMQFlag = sendMessageToRabbitMQ(orderNo);
        log.debug("给rabbitmq发送,延迟消息,订单号:{},结果:{}",orderNo,sendRabbitMQFlag);


        return orderNo;
    }

    public UserInfoVO getUserInfo(Integer userId){
        JsonResult<UserInfoVO> userInfo = userClient.getUserInfo(userId);
        if (userInfo!=null){
            return userInfo.getData();
        }
        return null;
    }

    public StationInfoVO getStationInfo(Integer gunId){
        JsonResult<StationInfoVO> stationInfo = deviceClient.getStationInfo(gunId);
        if(stationInfo!=null){
            return stationInfo.getData();
        }
        return null;
    }

    public Boolean updateGunStatus(Integer gunId){
        GunStatusUpdateParam gunStatusUpdateParam = new GunStatusUpdateParam();
        gunStatusUpdateParam.setGunId(gunId);
        gunStatusUpdateParam.setStatus(GunStatusConstants.GUN_STATUS_BUSY);//1空闲，2使用中，3故障，4离线
        JsonResult<Boolean> booleanJsonResult = deviceClient.updateGunStatus(gunStatusUpdateParam);
        Boolean gunStatusUpdateFlag = booleanJsonResult.getData();
        return gunStatusUpdateFlag;
    }

    public String getOrderNo(){
        //100000_2023-09-22 11:57:29_35
        Date date = new Date();
        SimpleDateFormat sdfLocal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strTime = sdfLocal.format(date);
        Random random = new Random();
        int num1 = random.nextInt(100);//[0,100)
        int num2 = random.nextInt(100);
        String numStr = num1 + "_" + num2;
        return "100000_"+strTime+numStr;
    }
    public void startCharging(String orderNo,Integer pileId,Integer gunId,Integer userId,Integer vehicleId){
        //此处还需要用户id
        String topic = MqttConstants.TOPIC_START_CHARGING_PREFIX+pileId;//eg:/topic/start/5
        ChargingDto chargingDto = new ChargingDto();
        chargingDto.setOrderNo(orderNo);
        chargingDto.setPileId(pileId);
        chargingDto.setGunId(gunId);
        chargingDto.setUserId(userId);
        chargingDto.setVehicleId(vehicleId);
        chargingDto.setMsg(ChargingConstants.START_CHARGING);
        String jsonMsg = JacksonUtil.toJson(chargingDto);
        mqttProducer.send(topic,jsonMsg);
    }

    public Boolean sendMessageToRabbitMQ(String orderNo){
        OrderMQPO orderMQPO = new OrderMQPO();
        orderMQPO.setOrderNo(orderNo);
        rabbitMQProducer.send(orderMQPO);
        return true;
    }
}
